package com.example.idachuappone.order.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.idachuappone.R;
import com.example.idachuappone.cook.action.CookAction;
import com.example.idachuappone.cook.entity.CookResult;
import com.example.idachuappone.cook.item.CookDetailActivity;
import com.example.idachuappone.person.entity.Order;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.Utils;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

public class MyOrderAdapter extends BaseAdapter {
	private BitmapUtils bu;
	private CallBack callBack;
	private Context context;
	private CookAction cookAction;
	private List<Order> list;

	class AnonymousClass_1 implements OnClickListener {
		private final/* synthetic */Order val$order;

		AnonymousClass_1(Order order) {
			this.val$order = order;
		}

		@Override
		public void onClick(View v) {
			MyOrderAdapter.this.callBack.btnClear(this.val$order.getId());
		}
	}

	class AnonymousClass_2 implements OnClickListener {
		private final/* synthetic */Order val$order;

		AnonymousClass_2(Order order) {
			this.val$order = order;
		}

		@Override
		public void onClick(View v) {
			MyOrderAdapter.this.callBack.btnPayOk(this.val$order.getId());
		}
	}

	class AnonymousClass_3 implements OnClickListener {
		private final/* synthetic */Order val$order;

		AnonymousClass_3(Order order) {
			this.val$order = order;
		}

		@Override
		public void onClick(View v) {
			MyOrderAdapter.this.callBack.btnClear(this.val$order.getId());
		}
	}

	class AnonymousClass_4 implements OnClickListener {
		private final/* synthetic */Order val$order;

		AnonymousClass_4(Order order) {
			this.val$order = order;
		}

		@Override
		public void onClick(View v) {
			MyOrderAdapter.this.callBack.btnClear(this.val$order.getId());
		}
	}

	class AnonymousClass_5 implements OnClickListener {
		private final/* synthetic */Order val$order;

		AnonymousClass_5(Order order) {
			this.val$order = order;
		}

		@Override
		public void onClick(View v) {
			MyOrderAdapter.this.callBack.btnFinish(this.val$order.getId());
		}
	}

	class AnonymousClass_6 implements OnClickListener {
		private final/* synthetic */Order val$order;

		AnonymousClass_6(Order order) {
			this.val$order = order;
		}

		@Override
		public void onClick(View v) {
			MyOrderAdapter.this.callBack.btnComment(this.val$order.getKitchener(), this.val$order.getId());
		}
	}

	class AnonymousClass_8 implements OnClickListener {
		private final/* synthetic */Order val$order;

		AnonymousClass_8(Order order) {
			this.val$order = order;
		}

		@Override
		public void onClick(View v) {
			String result = MyOrderAdapter.this.cookAction.isCookService(this.val$order.getKitchener());
			if (result == null) {
				Intent intent = new Intent(MyOrderAdapter.this.context, CookDetailActivity.class);
				intent.putExtra("id", this.val$order.getKitchener().getId());
				MyOrderAdapter.this.context.startActivity(intent);
				return;
			}
			MainToast.show(MyOrderAdapter.this.context, result, 0);
		}
	}

	class AnonymousClass_9 implements OnClickListener {
		private final/* synthetic */Order val$order;

		AnonymousClass_9(Order order) {
			this.val$order = order;
		}

		@Override
		public void onClick(View v) {
			String result = MyOrderAdapter.this.cookAction.isCookService(this.val$order.getKitchener());
			if (result == null) {
				Intent intent = new Intent(MyOrderAdapter.this.context, CookDetailActivity.class);
				intent.putExtra("id", this.val$order.getKitchener().getId());
				MyOrderAdapter.this.context.startActivity(intent);
				return;
			}
			MainToast.show(MyOrderAdapter.this.context, result, 0);
		}
	}

	public static interface CallBack {
		void btnClear(String str);

		void btnComment(CookResult cookResult, String str);

		void btnFinish(String str);

		void btnPayOk(String str);
	}

	private class ViewHolder {
		private Button btn_chulizhong_clear;
		private Button btn_comment;
		private Button btn_daichuli_clear;
		private Button btn_fuwuzhong_ok;
		private Button btn_pay_clear;
		private Button btn_pay_ok;
		private ImageView img_greed;
		private ImageView img_head;
		private LinearLayout ll_fuwuzhong;
		private LinearLayout ll_pay;
		private TextView tv_money;
		private TextView tv_name;
		private TextView tv_package;
		private TextView tv_state;
		private TextView tv_time;

		private ViewHolder() {
		}
	}

	public MyOrderAdapter(Context context, CallBack callBack) {
		this.context = context;
		this.callBack = callBack;
		this.bu = BitmapUtilHelper.createBitmapUtil(context);
		this.cookAction = new CookAction();
	}

	public void setList(List<Order> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return this.list == null ? 0 : this.list.size();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public Object getItem(int position) {
		return this.list == null ? null : (Order) this.list.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LinearLayout.inflate(this.context, R.layout.simple_my_order_two, null);
			viewHolder = new ViewHolder();
			viewHolder.tv_package = (TextView) convertView.findViewById(R.id.tv_package);
			viewHolder.tv_state = (TextView) convertView.findViewById(R.id.tv_state);
			viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			viewHolder.img_greed = (ImageView) convertView.findViewById(R.id.img_greed);
			viewHolder.img_head = (ImageView) convertView.findViewById(R.id.img_head);
			viewHolder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		Order order = this.list.get(position);
		viewHolder.ll_pay = (LinearLayout) convertView.findViewById(R.id.ll_pay);
		viewHolder.btn_pay_clear = (Button) convertView.findViewById(R.id.btn_pay_clear);
		viewHolder.btn_pay_clear.setOnClickListener(new AnonymousClass_1(order));
		viewHolder.btn_pay_ok = (Button) convertView.findViewById(R.id.btn_pay_ok);
		viewHolder.btn_pay_ok.setOnClickListener(new AnonymousClass_2(order));
		viewHolder.btn_daichuli_clear = (Button) convertView.findViewById(R.id.btn_daichuli_clear);
		viewHolder.btn_daichuli_clear.setOnClickListener(new AnonymousClass_3(order));
		viewHolder.btn_chulizhong_clear = (Button) convertView.findViewById(R.id.btn_chulizhong_clear);
		viewHolder.btn_chulizhong_clear.setOnClickListener(new AnonymousClass_4(order));
		viewHolder.ll_fuwuzhong = (LinearLayout) convertView.findViewById(R.id.ll_fuwuzhong);
		viewHolder.btn_fuwuzhong_ok = (Button) convertView.findViewById(R.id.btn_fuwuzhong_ok);
		viewHolder.btn_fuwuzhong_ok.setOnClickListener(new AnonymousClass_5(order));
		viewHolder.btn_comment = (Button) convertView.findViewById(R.id.btn_comment);
		viewHolder.btn_comment.setOnClickListener(new AnonymousClass_6(order));
		hideButton(viewHolder);
		String state = "";
		if (Integer.parseInt(order.getStatus()) == 1) {
			viewHolder.btn_daichuli_clear.setVisibility(0);
			state = "\u5904\u7406\u4e2d";
		} else if (Integer.parseInt(order.getStatus()) == 2) {
			viewHolder.btn_chulizhong_clear.setVisibility(0);
			state = "\u5df2\u5904\u7406";
		} else if (Integer.parseInt(order.getStatus()) == 3) {
			if (order.getKitchener() == null || order.getKitchener().getId() == null
					|| order.getKitchener().getId().equals("null")) {
				state = "\u5df2\u5b8c\u6210";
			} else if (Integer.parseInt(order.getIs_commented()) == 0) {
				viewHolder.btn_comment.setVisibility(0);
				state = "\u5df2\u5b8c\u6210";
			} else {
				state = "\u5df2\u5b8c\u6210";
			}
		} else if (Integer.parseInt(order.getStatus()) == 4) {
			state = "\u5df2\u53d6\u6d88";
		} else if (Integer.parseInt(order.getStatus()) == 5) {
			viewHolder.ll_pay.setVisibility(0);
			state = "\u5f85\u652f\u4ed8";
		} else if (Integer.parseInt(order.getStatus()) == 6) {
			viewHolder.ll_fuwuzhong.setVisibility(0);
			state = "\u670d\u52a1\u4e2d";
		}
		viewHolder.tv_state.setText(state);
		if (Integer.parseInt(order.getPackage_id()) == 6) {
			viewHolder.tv_name.setText("\u5ba2\u670d\u8054\u7cfb\u5e76\u63a8\u8350\u53a8\u5e08");
		} else if (order.getKitchener() == null || order.getKitchener().getId() == null
				|| order.getKitchener().getId().equals("null")) {
			viewHolder.tv_name.setText("\u5ba2\u670d\u8054\u7cfb\u5e76\u63a8\u8350\u53a8\u5e08");
			viewHolder.img_greed.setImageResource(-1);
			viewHolder.img_head.setImageResource(R.drawable.order_two_chief);
		} else {
			viewHolder.tv_name.setText(order.getKitchener().getName());
			this.bu.display(viewHolder.img_greed, order.getKitchener().getRecipe().get(0).getImg_small());
			this.bu.display(viewHolder.img_head, order.getKitchener().getHead_small(), new BitmapLoadCallBack<View>() {
				@Override
				public void onLoadCompleted(View view, String arg1, Bitmap bitmap, BitmapDisplayConfig arg3,
						BitmapLoadFrom arg4) {
					((ImageView) view).setImageBitmap(BitmapUtilHelper.getCroppedRoundBitmap(bitmap,
							Utils.dip2px(MyOrderAdapter.this.context, 16.0f)));
				}

				@Override
				public void onLoadFailed(View arg0, String arg1, Drawable arg2) {
				}
			});
			viewHolder.tv_name.setOnClickListener(new AnonymousClass_8(order));
			viewHolder.img_head.setOnClickListener(new AnonymousClass_9(order));
		}
		if (Double.valueOf(order.getPackage_price()).doubleValue() == 0.0d) {
			viewHolder.tv_package.setText(order.getPackage_name());
		} else {
			viewHolder.tv_package.setText(order.getPackage_name() + "/" + order.getPackage_price() + "\u5143");
		}
		viewHolder.tv_time.setText("\u7528\u9910\u65f6\u95f4\uff1a" + ComUtil.FormatCalenderTime1(order.getDate()));
		viewHolder.tv_money.setText("\uffe5" + order.getPay_price());
		return convertView;
	}

	private void hideButton(ViewHolder viewHolder) {
		viewHolder.ll_pay.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		viewHolder.btn_daichuli_clear.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		viewHolder.btn_chulizhong_clear.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		viewHolder.ll_fuwuzhong.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		viewHolder.btn_comment.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
	}
}
